/*
    ProgFrame extends JFrame
        :This is the main GUI object which holds all the panels that the user interacts with
        :From the looks of it, a Splash screen would be created through the main frame
        :I won't know how this works for sure until I get a chance to do some tests.
        Attributes
            String viewstate
                :this represents the state of the view
                :IE what is the user looking at right now (splash, main menu, login, stats, game, etc)
        Methods
            changeState(String)
                :this is to change the current Jpanel being viewed
*/

//import anything we need
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Component;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

public class ProgFrame extends JFrame implements WindowListener {
	
	//declare a String to represent what is currently visible
	public String viewState;
	//declare a panel we will use as our customized content pane
	private JPanel content;
	//declare a layered pane we will use for layering different visul elements
	public ManagedLayeredPane layers;
	//declare objects list representing the other views available
	private ArrayList<JPanel> views;
	
	//define any useful constants
	public static final Dimension framesize = new Dimension(500, 375);
	
	
	//here is our constructor
	public ProgFrame() {
		
		//we are extending, so call our ancestor 
		super();
		
		//set a reasonable size to match the ratio of our splash image
		this.setPreferredSize(ProgFrame.framesize);
		//setup title
		this.setTitle("Checkers3D");
		//setup dispose on close
		//this works the same as exit and should also allow this frame to imbed into an applet
		//this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		//nvm that, we will have close handled by a window listener
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		//setup listener to catch when the user tries to close
		this.addWindowListener(this);
		//setup icon
		try{
			this.setIconImage(ImageIO.read(new File("images\\SplashIcon.jpg")));
		}catch (IOException EXC){
			//if icon cannot be setup, load the default
			this.setIconImage(new UIController.NotFoundImage(10,10));
		}catch (IllegalArgumentException EXC){
			//something went wrong, load the default
			this.setIconImage(new UIController.NotFoundImage(10,10));
		}
		
		//setup our content pane
		//use a simple border layout with Center that will stretch to fit the frame
		//allow the sub components to control the size rather than setting it here
		content = new JPanel(new GridLayout(1,1));
		//content.setOpaque(true);
		//content.setVisible(true);
		content.setBackground(Color.BLUE);
		this.setContentPane(content);
		
		//setup our layered pane
		// need to make sure that our height is scaled back to account for page start header of the frame
		Rectangle layerbounds = new Rectangle();
		layerbounds.width = ProgFrame.framesize.width;
		layerbounds.height = ProgFrame.framesize.height;
		layers = new ManagedLayeredPane(10.0, 4.0, layerbounds);
		layers.startConsole();
		layers.startPopup();
		//layers.setBackground(Color.RED);
		layers.setOpaque(true);
		this.setLayeredPane(layers);
		
		//setup our views list
		views = new ArrayList<JPanel>();
		//call the constructors of the different views
		views.add(new MainView());
		views.add(new GameView());
		views.add(new GameLogon());
		views.add(new GameLoad());
		views.add(new StatsLogon());
		views.add(new StatsView());
		views.add(new AdminLogon());
		views.add(new AdminView());
		views.add(new GameReview());
		views.add(new GameComplete());
		//the following will not be needed till later builds
		/*
		*/
		
		//setup the view state and set the initial view to mainview
		viewState = "";
		//this.changeState("MainView");
		
		//set our frame position
		this.setLocation(50, 50);
		//we can make our frame visible at this point
		this.setVisible(true);
		//go ahead and pack our frame to adhere to preferred sizes
		this.pack();
		
	}
	
	
	//method that can be called to change the current JPanel being viewed
	public boolean changeState(String newState) {
		
		System.out.println("PROGFRAME: looking for views of type " + newState);
		//determine what panel to switch to if any
		search_views: for(int i=0; i<this.views.size(); ++i){
			if (this.views.get(i).getClass().getName().equals(newState)){
				System.out.println("PROGFRAME: found view that matches...");
				//clear previous
				//content.removeAll();
				this.layers.removeViews();
				//add
				//this.views.get(i).setVisible(true);
				//Dimension viewsize = this.getSize(null);
				//this.views.get(i).setBounds(0, 0, viewsize.width, viewsize.height);
				this.layers.addView(this.views.get(i));
				//layers.add(this.views.get(i), new Integer(ProgFrame.VIEW_LAYER));
				
				/*
				JPanel dummy = new JPanel();
				dummy.setBackground(Color.YELLOW);
				dummy.setBounds((int)(viewsize.width/2), (int)(viewsize.height/2), (int)(viewsize.width/2), (int)(viewsize.height/2));
				layers.add(dummy, new Integer(ProgFrame.POPUP_LAYER));
				*/
				
				//layers.setBackground(Color.RED);
				//layers.setVisible(true);
				//this.layers.validate();
				//this.layers.repaint();
				/*
				if (this.views.get(i).getPreferredSize() != null){
					layers.setPreferredSize(this.views.get(i).getPreferredSize());
				}else{
					System.out.println("PROGFRAME: preferred size of " + this.views.get(i).getClass().getName() + " is null!");
				}
				System.out.println("PROGFRAME: layers prefers size of " + layers.getPreferredSize());
				this.setLayeredPane(layers);
				*/
				//content.add(layers);
				//content.setBackground(Color.BLUE);
				//this.setContentPane(content);
				//content.revalidate();
				//content.repaint();
				return true;
			}
		}
		
		//if we reach this point then the String passed does not match any existing panels
		return false;
	}
	
	//method used to get an object reference to a particular view panel
	public JPanel getViewPanel(String panelName) {
		
		//find the first view that matches if any
		search_views: for(int i=0; i<this.views.size(); ++i){
			if (this.views.get(i).getClass().getName().equals(panelName)){
				return this.views.get(i);
			}
		}
		
		//if we reach this point then the String passed does not match any existing panels
		return null;
	}
	
	//hidden convenience method for asking the user a yes/no question
	private boolean askYNQuestion(String question, String title) {
		
		int response = JOptionPane.showConfirmDialog(this, 
			question, title, JOptionPane.YES_NO_OPTION,
			JOptionPane.QUESTION_MESSAGE);
		
		if (response == JOptionPane.YES_OPTION){
			return true;
		}else{
			return false;
		}
	}
	
	//overridden window listener methods
	public void windowOpened(WindowEvent EVE) {}
	public void windowActivated(WindowEvent EVE) {}
	public void windowDeactivated(WindowEvent EVE) {}
	public void windowIconified(WindowEvent EVE) {}
	public void windowDeiconified(WindowEvent EVE) {}
	public void windowClosing(WindowEvent EVE) {
		
		//verify close
		if (askYNQuestion("Are you sure you want to exit the program?", "Verify Close")){
			//close properly
			UIController.exit();
		}else{
			//do nothing
		}
		
	}
	public void windowClosed(WindowEvent EVE) {
		
		//close properly
		//UIController.exit();
		
	}
	
	
}